import 'package:flutter/material.dart';
import 'package:wallpaper/components/scan_code/scan_code.dart';
import 'package:wallpaper/pages/navbar/child/bluetooth.dart';
import 'package:wallpaper/pages/navbar/child/selected.dart';
import 'package:wallpaper/pages/navbar/child/wallpaper.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  late TabController tabController;
  late PageController pageController;
  String searchWord = '搜点什么···';

  List<Widget> pageviews = [Wallpaper(), Selected(), Bluetooth()];

  @override
  void initState() {
    super.initState();
    tabController = TabController(length: 3, vsync: this);
    pageController = PageController(initialPage: 0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          toolbarHeight: 0,
          elevation: 0,
          backgroundColor: Theme.of(context).colorScheme.primaryContainer,
          surfaceTintColor: Theme.of(context).colorScheme.primaryContainer,
        ),
        body: Column(
          children: [
            _buildHeader(context),
            _buildTap(context),
            Expanded(
              // 使用 Expanded 包裹 PageView
              child: PageView(
                controller: pageController, // 绑定 TabController
                onPageChanged: (index) {
                  setState(() {
                    tabController.animateTo(index);
                  });
                },
                children: pageviews,
              ),
            )
          ],
        ));
  }

  Widget _buildHeader(BuildContext context) => Container(
        padding: const EdgeInsets.all(10),
        color: Theme.of(context).colorScheme.primaryContainer,
        child: Row(children: [
          Image.asset('lib/assets/images/icon.png', width: 30, height: 30),
          Expanded(
            child: Container(
              margin: const EdgeInsets.only(left: 10, right: 10),
              height: 28,
              decoration: BoxDecoration(
                  color: Theme.of(context).colorScheme.surface,
                  borderRadius: const BorderRadius.all(Radius.circular(15))),
              child: Flex(direction: Axis.horizontal, children: [
                const SizedBox(width: 10),
                const Icon(Icons.search, color: Colors.grey),
                Text(searchWord,
                    style: TextStyle(fontSize: 14, color: Colors.grey)),
              ]),
            ),
          ),
          InkWell(
            onTap: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => ScanCode(onScanCompleted: (value) {
                            setState(() {
                              searchWord = value;
                            });
                          })));
            },
            child: Icon(
              Icons.qr_code_scanner,
              color: Theme.of(context).colorScheme.onSurface,
              size: 28,
            ),
          ),
        ]),
      );

  Widget _buildTap(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      color: Theme.of(context).colorScheme.primaryContainer,
      child: TabBar(
        tabs: [
          Tab(
            text: '壁纸',
          ),
          Tab(
            text: '精选',
          ),
          Tab(
            text: '蓝牙',
          )
        ],
        controller: tabController,
        indicatorColor: Theme.of(context).colorScheme.onSurface,
        indicatorWeight: 3,
        labelColor: Theme.of(context).colorScheme.onSurface,
        unselectedLabelColor: Colors.grey,
        // 去除下划线
        dividerColor: Colors.transparent,
        onTap: (index) {
          setState(() {
            pageController.jumpToPage(index);
          });
        },
      ),
    );
  }
}
